<?php

use Biz\model\GoodsTestActivity\FineGoodsJoinRecordModel;
use Biz\model\GoodsTestActivity\FineGoodsModel;
use Biz\model\GoodsTestActivity\FineGoodsMsgModel;
use Biz\service\GoodsTestActivity\FineGoodsService;
use Doraemon\cache\UserCounter;
use Doraemon\service\Follow;
use Doraemon\service\User;
use Doraemon\model\Phone as PhoneModel;
use hhz\constant\basic\UserExtendPhoneScene;
use Doraemon\tools\Log;
use Biz\cache\brand\BrandShowActivity;

/**
 * Description: 创建收货地址
 * Created by PhpStorm.
 * User: 玉坤
 * Date: 2021/6/16
 * Time: 14:33
 */
class CreateAddressAction extends ActionPlugin
{
    public const USER_LEVEL = self::LEVEL_REGISTER;


    public function main()
    {
        $uid = $this->uid;
        if (!$uid) {
            throw new \Exception("请登录重试！");
        }
        $name = $this->getRequest()->getPost('name', '');
        $tel = $this->getRequest()->getPost('tel', 0);
        $address = $this->getRequest()->getPost('address', '');
        $is_share = $this->getRequest()->getPost('is_share', FineGoodsJoinRecordModel::NO_SHARE);
        $fine_goods_id = $this->getRequest()->getPost('fine_goods_id', 0);

        if (!$name || !$tel || !$address || !$fine_goods_id) {
            throw new \Exception('非法传参');
        }

        $encryptPhoneId = PhoneModel::addExtendPhone($tel,UserExtendPhoneScene::GOODS_TEST_JOIN_PHONE);
        if (!$encryptPhoneId) {
            Log::error('添加手机号失败', [$tel, $encryptPhoneId]);
            throw new \Exception('活动太火爆了，请稍后再试');
        }
        $key = "fine_goods_test_address_" . $uid;
        $islock = BrandShowActivity::isLock($key, 5);
        if ($islock) {
            throw new \Exception('请不要频繁提交');
        }

        $info = FineGoodsService::getActIngInfo($fine_goods_id,
            ['brand_id', 'basic_id', 'id', 'brand_name', 'basic_title']);
        if (!$info) {
            throw new \Exception('此活动已结束申请或已下架！');
        }
        $fans = UserCounter::getOne($uid)['fans'] ?? 0;

        $user_type = FineGoodsModel::getUserGrade($fans);

        $address_id = FineGoodsJoinRecordModel::first(['uid' => $uid, 'fine_goods_id' => $fine_goods_id], 'id');
        if ($address_id) {
            throw new \Exception('您已完成申请!');
        }
        $data['uid'] = $uid;
        $data['fans_num'] = $fans;
        $data['fans_num_grade'] = $user_type;
        $data['fine_goods_id'] = $fine_goods_id;
        $data['basic_id'] = $info['basic_id'] ?? 0;
        
        $data['encrypt_phone_id'] = $encryptPhoneId;
        $data['name'] = $name;
        $data['brand_id'] = $info['brand_id'] ?? 0;
        $data['address'] = $address;
        $data['dynamic_sync'] = $is_share;
        FineGoodsJoinRecordModel::insert($data);
        $id = FineGoodsJoinRecordModel::id();
        if (!$id) {
            throw new \Exception('提交失败请重试！');
        }
//        $data['content'] = "参与了" . $info['brand_name'] . $info['basic_title'] . "的免费测评";
        //添加消息记录
        FineGoodsService::addMsgRecord(FineGoodsMsgModel::MSG_APPLY, [$data]);

        try {
            Follow::FollowMultiUsers($uid, [User::HAOHAOZHUFULIGUAN_UID], 'new_product');//关注好好住福利官
        } catch (\Exception $e) {
        }
        $result = FineGoodsService::getWishMsg($info['brand_name']);
        $this->setSuccess($result);

    }

}